package main

import (
	"net"
	"fmt"
	"bufio"
	"os"
	"strings"
)

func CheckError1(err error){
	if err != nil{
/*		fmt.Println(err.Error())
		os.Exit(1)*/
		panic(err)
	}
}

func MessageSend(conn net.Conn){
	var input string
	for  {
		//接收系统标准输入
		reader := bufio.NewReader(os.Stdin)
		data, _, _ := reader.ReadLine()
		input = string(data)

		//
		if strings.ToUpper(input) == "EXIT" {
			conn.Close()
			break
		}

		_, err :=conn.Write([]byte(input))         //Write()就是发送至服务端

		if err != nil {
			conn.Close()
			fmt.Println("client error", err.Error())
			break
		}

	}
}

func main(){
	conn, err :=net.Dial("tcp","127.0.0.1:8080")
	CheckError1(err)

	defer conn.Close()

	//conn.Write([]byte("Hello chatoom"))
	go MessageSend(conn)

	buf := make([]byte, 1024)
	for {
		num,err :=conn.Read(buf)                   //Read()就是读取服务端的发送来的信息
		if err != nil {
			fmt.Println("你已退出")
			os.Exit(0)
		}
		fmt.Println("received server message content:" +string(buf[0:num]))
	}
	//time.Sleep(100000000000000)

	fmt.Println("Client program end!")
}